package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/7.
 */
public class DoAppointTransfer extends AbstractTransfer {

    public DoAppointTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String hospitalCode = (String) params.get("hospitalCode");
        String doctorCode = (String) params.get("doctorCode");
        String deptCode = (String) params.get("deptCode");
        String scheduleDate = (String) params.get("scheduleDate");
        String timeDesc = (String) params.get("timeDesc");
        String regFee = (String) params.get("regFee");
        String clinicFee = (String) params.get("clinicFee");
        String otherFee = (String) params.get("otherFee");
        String clinicCharge = (String) params.get("clinicCharge");
        String phone = (String) params.get("phone");
        String feeType = (String) params.get("feeType");
        String idCard = (String) params.get("idCard");
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        String chargeType = (String) params.get("chargeType");
        String sex = params.get("sex").toString();
        if ("1".equals(sex)) {
            sex = "男";
        }
        if ("2".equals(sex)) {
            sex = "女";
        }
        String name = (String) params.get("name");
        String dateOfBirth = (String) params.get("dateOfBirth");
        Document document = getTplDoc("DoAppoint");
        Element dataRow = document.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRow.addAttribute("CLINIC_VISIT_DATE", scheduleDate).addAttribute("TIME_DESC", timeDesc);
        dataRow.addAttribute("REGIST_FEE", regFee).addAttribute("CLINIC_FEE", clinicFee).addAttribute("OTHER_FEE", otherFee);
        dataRow.addAttribute("CLINIC_CHARGE", clinicCharge).addAttribute("FEE_TYPE", feeType).addAttribute("HOUR_START", beginTime);
        dataRow.addAttribute("HOUR_END", endTime).addAttribute("REGIST_DETAIL_ID", "").addAttribute("PATIENT_NAME", name);
        dataRow.addAttribute("SEX", sex).addAttribute("CHARGE_TYPE", chargeType).addAttribute("DATE_OF_BIRTH", dateOfBirth);
        dataRow.addAttribute("INSURANCE_NO", "").addAttribute("ID_NO", idCard).addAttribute("PHONE_NUMBER", phone);
        dataRow.addAttribute("CREDENTIALS_NO", idCard).addAttribute("CREDENTIALS_TYPE", "1").addAttribute("PATIENT_ID", "");
        dataRow.addAttribute("DEPT_CODE", deptCode).addAttribute("DOCTOR_CODE", doctorCode);
        return document.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new LinkedHashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            DoAppointInnerMessage doAppointInnerMessage = new DoAppointInnerMessage(dataRow);
            String arr[] = doAppointInnerMessage.getReportEndTime().split(" ");
            resMap.put("appointId", doAppointInnerMessage.getApptedId());
            resMap.put("reportStartTime", doAppointInnerMessage.getReportStartTime());
            resMap.put("reportEndTime", doAppointInnerMessage.getReportEndTime());
            resMap.put("appointDate",doAppointInnerMessage.getAppointScheduleTime());
            resMap.put("clinicType", doAppointInnerMessage.getClinicType());
            resMap.put("code", executeStatus);
            resMap.put("registOrder",doAppointInnerMessage.getRegistOrder());
            resMap.put("clinicLabel",doAppointInnerMessage.getClinicLabel());
            resMap.put("message", "预约成功!您的报到时间为" + doAppointInnerMessage.getReportStartTime().substring(0, doAppointInnerMessage.getReportStartTime().length() - 3) + "——" + arr[1].substring(0, arr[1].length() - 3) + "。" );

        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    private class DoAppointInnerMessage implements InnerMessage {

        private String clinicVisitDate;
        private String clinicLabel;
        private String timeDesc;
        private String hourStart;
        private String hourEnd;
        private String registOrder;
        private String apptedId;
        private String patientName;
        private String sex;
        private String chargeType;
        private String dateOfBirth;
        private String insuranceNo;
        private String idNo;
        private String phoneNumber;
        private String credentialsNo;
        private String credentialsType;
        private String appointDate;
        private String appointScheduleTime;
        private String reportStartTime;
        private String reportEndTime;
        private String guidelines;
        private String appointSurplus;
        private String clinicType;
        private String messageResult;


        public DoAppointInnerMessage(Element element) {

            this.clinicVisitDate = element.attributeValue("CLINIC_VISIT_DATE");
            this.clinicLabel = element.attributeValue("CLINIC_LABEL");
            this.timeDesc = element.attributeValue("TIME_DESC");
            this.hourStart = element.attributeValue("HOUR_START");
            this.hourEnd = element.attributeValue("HOUR_END");
            this.registOrder = element.attributeValue("REGIST_ORDER");
            this.apptedId = element.attributeValue("APPTED_ID");
            this.patientName = element.attributeValue("PATIENT_NAME");
            this.sex = element.attributeValue("SEX");
            this.chargeType = element.attributeValue("CHARGE_TYPE");
            this.dateOfBirth = element.attributeValue("DATE_OF_BIRTH");
            this.insuranceNo = element.attributeValue("INSURANCE_NO");
            this.idNo = element.attributeValue("ID_NO");
            this.phoneNumber = element.attributeValue("PHONE_NUMBER");
            this.credentialsNo = element.attributeValue("CREDENTIALS_NO");
            this.credentialsType = element.attributeValue("CREDENTIALS_TYPE");
            this.appointDate = element.attributeValue("APPOINT_DATE");
            this.appointScheduleTime = element.attributeValue("APPOINT_SCHEDULE_TIME");
            this.reportStartTime = element.attributeValue("REPORT_START_TIME");
            this.reportEndTime = element.attributeValue("REPORT_END_TIME");
            this.guidelines = element.attributeValue("GUIDELINES");
            this.appointSurplus = element.attributeValue("APPOINT_SURPLUS");
            this.clinicType = element.attributeValue("CLINIC_TYPE");
            this.messageResult = element.attributeValue("MESSAGE_RESULT");

        }

        public String getClinicVisitDate() {
            return clinicVisitDate;
        }

        public String getClinicLabel() {
            return clinicLabel;
        }

        public String getTimeDesc() {
            return timeDesc;
        }

        public String getHourStart() {
            return hourStart;
        }

        public String getHourEnd() {
            return hourEnd;
        }

        public String getRegistOrder() {
            return registOrder;
        }

        public String getApptedId() {
            return apptedId;
        }

        public String getPatientName() {
            return patientName;
        }

        public String getSex() {
            return sex;
        }

        public String getChargeType() {
            return chargeType;
        }

        public String getDateOfBirth() {
            return dateOfBirth;
        }

        public String getInsuranceNo() {
            return insuranceNo;
        }

        public String getIdNo() {
            return idNo;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public String getCredentialsNo() {
            return credentialsNo;
        }

        public String getCredentialsType() {
            return credentialsType;
        }

        public String getAppointDate() {
            return appointDate;
        }

        public String getAppointScheduleTime() {
            return appointScheduleTime;
        }

        public String getReportStartTime() {
            return reportStartTime;
        }

        public String getReportEndTime() {
            return reportEndTime;
        }

        public String getGuidelines() {
            return guidelines;
        }

        public String getAppointSurplus() {
            return appointSurplus;
        }

        public String getClinicType() {
            return clinicType;
        }

        public String getMessageResult() {
            return messageResult;
        }
    }


}
